package com.free.framework.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.free.framework.R;
import com.free.framework.base.BaseActivity;
import com.free.framework.lib.utils.ScreenUtils;

/**
 * @author 李博
 * @date 2016年11月02日
 * @desc
 */
public class FjrAppbar implements IAppbar {

    private Toolbar mToolbar;
    private TextView mTxtTitle;


    public FjrAppbar(Builder builder) {

        BaseActivity baseActivity = builder.getmBaseActivity();

        mToolbar = (Toolbar) LayoutInflater.from(baseActivity).inflate(R.layout.fjr_app_bar, null);

        mTxtTitle = (TextView) mToolbar.findViewById(R.id.txt_center);
        mTxtTitle.setTextColor(baseActivity.getResources().getColor(R.color.white));
        mToolbar.setBackgroundResource(R.color.colorPrimary);
        if (0 != builder.getTitleResId()) {
            if (builder.isInCenter) {
                mTxtTitle.setVisibility(View.VISIBLE);
                mTxtTitle.setText(builder.getTitleResId());
            } else {
                mTxtTitle.setVisibility(View.GONE);
                mToolbar.setTitle(builder.getTitleResId());
            }
        }


        if (null != builder.getTitleString()) {
            if (builder.isInCenter) {
                mTxtTitle.setVisibility(View.VISIBLE);
                mTxtTitle.setText(builder.getTitleString());
            } else {
                mTxtTitle.setVisibility(View.GONE);
                mToolbar.setTitle(builder.getTitleString());
            }
        }

        if (0 != builder.getRightTxtResId()) {
            addRightTextView(baseActivity, builder.getRightTxtResId(), builder.getRightClickListener());
        }

        if (0 != builder.getRightImgResId()) {
            addRightImageView(baseActivity, builder.getRightImgResId(), builder.getRightClickListener());
        }

        if (0 != builder.getBgColorResId()) {
            mToolbar.setBackgroundResource(builder.getBgColorResId());
        }

        if (0 != builder.getLeftImgResId()) {
            mToolbar.setNavigationIcon(builder.getLeftImgResId());
        }

        if (null != builder.getLeftClickListener()) {
            mToolbar.setNavigationOnClickListener(builder.getLeftClickListener());
        }

    }

    @SuppressLint("RtlHardcoded")
    private void addRightTextView(Context context, int rightTxtResId, View.OnClickListener listener) {
        TextView rightTxt = new TextView(context);
        rightTxt.setText(rightTxtResId);
        rightTxt.setOnClickListener(listener);

        Toolbar.LayoutParams lp = new Toolbar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.CENTER_VERTICAL | Gravity.RIGHT;
        mToolbar.addView(rightTxt, lp);
    }

    @SuppressLint("RtlHardcoded")
    private void addRightImageView(Context context, @DrawableRes int imgId, View.OnClickListener listener) {
        ImageView rightImg = new ImageView(context);
        rightImg.setImageResource(imgId);
        rightImg.setOnClickListener(listener);

        Toolbar.LayoutParams lp = new Toolbar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.gravity = Gravity.CENTER_VERTICAL | Gravity.RIGHT;
        mToolbar.addView(rightImg, lp);
    }


    @Override
    public void attachToActivity(BaseActivity baseActivity) {

        ViewGroup decorView = (ViewGroup) baseActivity.getWindow().getDecorView();
        View contentView = ((ViewGroup) decorView.findViewById(android.R.id.content)).getChildAt(0);

        LinearLayout parent = new LinearLayout(baseActivity);
        parent.setOrientation(LinearLayout.VERTICAL);
        parent.addView(mToolbar, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        ViewGroup viewParent = (ViewGroup) contentView.getParent();
        viewParent.removeView(contentView);
        parent.addView(contentView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        viewParent.addView(parent);
    }

    public static class Builder {

        private BaseActivity mBaseActivity;
        private boolean isInCenter;
        private int titleResId;

        private int leftImgResId;
        private View.OnClickListener leftClickListener;

        private int rightTxtResId;
        private int rightImgResId;
        private int bgColorResId;
        private View.OnClickListener rightClickListener;
        private String titleString;


        public Builder(BaseActivity mBaseActivity) {
            this.mBaseActivity = mBaseActivity;
        }

        public BaseActivity getmBaseActivity() {
            return mBaseActivity;
        }

        public void setmBaseActivity(BaseActivity mBaseActivity) {
            this.mBaseActivity = mBaseActivity;
        }

        public boolean isInCenter() {
            return isInCenter;
        }

        public Builder titleInCenter(boolean inCenter) {
            isInCenter = inCenter;
            return this;
        }

        public int getTitleResId() {
            return titleResId;
        }

        public Builder title(@StringRes int titleResId) {
            this.titleResId = titleResId;
            return this;
        }


        public Builder title(String titleString) {
            this.titleString = titleString;
            return this;
        }

        public String getTitleString() {
            return titleString;
        }

        public Builder leftImgResId(@DrawableRes int id) {
            this.leftImgResId = id;
            return this;
        }

        public Builder rightImage(@DrawableRes int id) {
            this.rightImgResId = id;
            return this;
        }

        public Builder rightClickListener(View.OnClickListener listener) {
            this.rightClickListener = listener;
            return this;
        }

        public int getLeftImgResId() {
            return leftImgResId;
        }

        public View.OnClickListener getLeftClickListener() {
            return leftClickListener;
        }

        public Builder setLeftClickListener(View.OnClickListener leftClickListener) {
            this.leftClickListener = leftClickListener;
            return this;
        }

        public int getRightImgResId() {
            return rightImgResId;
        }

        public int getRightTxtResId() {
            return rightTxtResId;
        }

        public void rightText(int rightTxtResId) {
            this.rightTxtResId = rightTxtResId;
        }

        public View.OnClickListener getRightClickListener() {
            return rightClickListener;
        }

        public Builder bgColor(@ColorRes int color) {
            this.bgColorResId = color;
            return this;
        }

        public int getBgColorResId() {
            return bgColorResId;
        }

        public FjrAppbar create() {
            return new FjrAppbar(this);
        }

    }

}
